﻿import com.mixmedia.control.BtnArrow;
/**
   ActionScript Class File -- Created with SAPIEN Technologies PrimalScript 3.1
   
   @class  
   @package
   @author Colin Leung
   @codehint 
   @example 
   @tooltip 
*/


class com.mixmedia.control.Scroller extends MovieClip{
	private var mcUpArrow:BtnArrow;
	private var mcDownArrow:BtnArrow;

	private var nTextVisibleLine:Number;
	
	public var maxIndex:Number;

	private var _index : Number = 0;

	private var oTarget : Object;

	public function Scroller(){//constructor
	}


	private function getTargetType():String{
		return (typeof(getTarget())=="movieclip")?((getTarget().mcType=="list")?"list":"movieclip"):'textfield';
	}

	private function getTarget():Object{
		return _parent[_name.substring(0,_name.indexOf("_scroll"))];
	}

	public function init():Void{
		oTarget = getTarget();
		switch (getTargetType()){
			case "movieclip":
				initMC();
				break;
			case "list":
				initList();
				break;
			case "textfield":
				initTxf();
				break;
		}
		mcUpArrow.enable = false;
		index = 0;
	};

	public function set index(arg:Number){
		if(arg>maxIndex){
			_index=maxIndex;
			mcDownArrow.enable = false;
			return;
		}
		if(arg < 0){
			_index=0;
			mcUpArrow.enable = false;
			return;
		}

		_index = arg;
		mcUpArrow.enable = true;
		mcDownArrow.enable = true;
		switch (getTargetType()){
			case "movieclip":
				getTarget()._y =-_index;	
				break;
			case "list":
				getTarget()._y = -_index*oTarget.listCellHeight;
				break;
			case "textfield":
				getTarget().scroll = _index+1;
				break;
		}
	}

	public function get index():Number{
		return _index;
	}

	private function initMC() : Void {
		trace(oTarget._height+','+oTarget.mask._height);

		var targetDisplayHeight = oTarget.mask._height;
		var targetHeight = oTarget._height;
		maxIndex = targetHeight-targetDisplayHeight+100;
	}

	private function initList() : Void {
		var targetDisplayHeight = oTarget.mask._height;
		var targetHeight = oTarget._height;
		maxIndex = oTarget.rowCount - Math.round(targetDisplayHeight/oTarget.listCellHeight)+1;
	}

	private function initTxf() : Void {
		// fill linebreak to for 1 more page
		var i;
		for(i=1;i<3;i++){
			oTarget.htmlText+="<br/>";
		}
		oTarget.scroll=1;
		var extraLine = oTarget.maxscroll-1;
		//targetDisplayHeight = oTarget.bottomScroll-oTarget.scroll+1;
		//targetHeight = targetDisplayHeight+extraLine;
		maxIndex = extraLine;
		
	}

}